package com.dm.user.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.dm.user.model.User;
import com.dm.user.util.HibernateUtil;


public class UserDao implements IUserDao {
	public void add(User user) {
		Session session = null;
		try {
			session = HibernateUtil.openSession();
			session.beginTransaction();
			
			session.save(user);
			
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			HibernateUtil.close(session);
		}
	}

	public User loadByUsername(String name) {
		Session session = null;
		User user = null;
		try {
			session = HibernateUtil.openSession();
			user = (User)session.createQuery("from User where username=?")
							.setParameter(0, name).uniqueResult();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			HibernateUtil.close(session);
		}
		return user;
	}

}
